package edu.sctu.xialei.june.day20150623one.json.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * Created by Administrator on 2015/6/23.
 */
public class JsonParse {

    private DataStore dataStore = new DataStore();

    public DataStore parse(String jsonString){
        JSONObject mainJsonObject = JSON.parseObject(jsonString);

        dataStore.error = mainJsonObject.getIntValue("error");
        JSONArray jsonArray = JSON.parseArray(mainJsonObject.getString("list"));

        for (int i = 0; i < jsonArray.size(); i++) {
            GoodData goodData = new GoodData();

            goodData.goods_device = jsonArray.getJSONObject(i).getString("goods_device");
            goodData.goods_introduce = jsonArray.getJSONObject(i).getString("goods_introduce");
            goodData.goods_name = jsonArray.getJSONObject(i).getString("goods_name");
            goodData.goods_price = Float.parseFloat(jsonArray.getJSONObject(i).getString("goods_price"));

            dataStore.list.add(goodData);
        }

        return dataStore;
    }
}
